package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.LikesBehaviorService;
import com.heima.common.article.HotArticleConstants;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.LikesBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.dtos.NewBehaviorDTO;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author huan
 * @serial 每天一百行, 致敬未来的自己
 * @Description
 */
@Service
@Slf4j
public class LikesBehaviorServiceImpl implements LikesBehaviorService {
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Override
    public ResponseResult like(LikesBehaviorDTO dto) {
        //点赞需要登录
        ApUser apUser = AppThreadLocalUtils.getUser();
        if (apUser == null) {
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }
        //获取当前登录用户id
        Integer userId = apUser.getId();
        ApBehaviorEntry entry = apBehaviorEntryService.findByUserIdOrEquipmentId(userId, dto.getEquipmentId());
        if (entry == null) {
            //entry == null 代表 UserId 与 EquipmentId 同时为null
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN, "请使用账户或设备进行登录");
        }
        //获取行为查询条件
        Query query = Query.query(Criteria.where("entryId").is(entry.getId()).and("articleId").is(dto
                .getArticleId()));
        //点赞
        if (dto.getOperation().equals(ApLikesBehavior.Operation.LIKE.getCode())) {
            //查询用户是否已经点赞
            ApLikesBehavior likesBehavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
            if (likesBehavior != null) {
                CustException.cust(AppHttpCodeEnum.NO_OPERATOR_AUTH, "该文章已经点赞过了哦!");
            }
            //没有点赞,进行点赞 保存 点赞信息至mongodb
            ApLikesBehavior apLikesBehavior = ApLikesBehavior.builder().entryId(entry.getId())
                    .articleId(dto.getArticleId()).type(dto.getType())
                    .operation(dto.getOperation()).createdTime(new Date()).build();
            //保存数据库mongodb
            mongoTemplate.save(apLikesBehavior);
        } else {
            //取消点赞
            mongoTemplate.remove(query, ApLikesBehavior.class);
        }
        //*****添加文章点赞-发送消息******
        NewBehaviorDTO mess = new NewBehaviorDTO();
        mess.setType(NewBehaviorDTO.BehaviorType.LIKES);
        mess.setArticleId(dto.getArticleId());
        mess.setAdd(dto.getOperation() == 0 ? 1 : -1);
        rabbitTemplate.convertAndSend(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE, JSON.toJSONString(mess));
        log.info("点赞行为 发送消息 ==>{}", JSON.toJSONString(mess));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
